﻿using System;
using System.Threading;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace AppMain
{
    class LogData
    {
        private static LogData _this = new LogData();
        public Mutex _logInUse = new Mutex();
        private StreamWriter _log;
        private DateTime _date;

        // called when the class is instantiated. All initialization and setup should
        // be done in this routine.
        // Because the log file is only established once, it is configured here.
        // The logging takes place below, but it is only necessary to initilize the log
        // once. Because this is a Singleton Class, the Log is only configured a single
        // time
        LogData()
        {
            _logInUse.WaitOne();
            if (Directory.Exists(foldername) == false)
            {
                Directory.CreateDirectory(foldername);
            }

            _date = DateTime.Now;
            string formatDate = "yyyyMMdd";
            try
            {
                _log = new StreamWriter(string.Format("{0}\\{1}_{2}",
                                                            foldername,
                                                            _date.ToString(formatDate),
                                                            filename),
                                                            true);
            }
            catch (SystemException error)
            {
                var mb = MessageBox.Show(error.Message);
            }

            _logInUse.ReleaseMutex();
        }

        // class destructor, close the log
        ~LogData()
        {
            // Apparently C# handles closing the file without doing it in the destructor
            //_log.Close();
        }

        // Create Singleton for LogData. Hence all classes will reference the same object
        public static LogData GetObject
        {
            get
            {
                return _this;
            }
        }


        private string foldername = "LogFiles";
        private string filename = "Application.txt";

        public void Write(string info)
        {
            // if a Thread calls this function, it will hit this mutex and
            // stop until the other application has completed it's write
            _logInUse.WaitOne();
            DateTime date = DateTime.Now;
            // Make a new log file each day, but only if there is data for in it.
            if ((date.Subtract(_date)).Days >= 1)
            {
                _log.Close();
                string formatDate = "yyyyMMdd";
                _log = new StreamWriter(string.Format("{0}\\{1}_{2}",
                                                            foldername,
                                                            _date.ToString(formatDate),
                                                            filename),
                                                            true);
            }
            _log.WriteLine("{0}(UTC) {1}", DateTime.Now.ToUniversalTime().ToString("u"), info);
            _logInUse.ReleaseMutex();
        }

        public void RecordAll(string info, DateTime starttime, string txname)
        {
            //WORK IN PROGRESS - DO NOT USE!!!

            if (Directory.Exists(foldername) == false)
            {
                Directory.CreateDirectory(foldername);
            }

            string formatDate = "yyyyMMdd-hh-mm";
            string path = string.Format("{0}\\{1}_{2}.csv",
                                        foldername,
                                        starttime.ToString(formatDate),
                                        txname);

            if (File.Exists(path))
            {
                //File already exists; just append data
                StreamWriter log = new StreamWriter(path, true);
                log.WriteLine("{0}(UTC) {1}", DateTime.Now.ToUniversalTime().ToString("u"), info);
                log.Close();
            }
            else
            {
                //File is new; add header row
                StreamWriter log = new StreamWriter(path, true);
                log.WriteLine("{0}(UTC) {1}", DateTime.Now.ToUniversalTime().ToString("u"), info);
                log.Close();
            }

            

            
        }
        public void Close()
        {
            _log.Close();
        }
    }

}
